package com.smartstate.IOC.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smartstate.IOC.dto.PageDTO;
import com.smartstate.IOC.entity.CarbonContribution;
import com.smartstate.IOC.mapper.ContributionMapper;
import com.smartstate.IOC.service.ContributionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Transactional
@Service
@Slf4j
public class ContributionServiceImpl extends ServiceImpl<ContributionMapper, CarbonContribution> implements ContributionService {


    @Autowired
    ContributionMapper contributionMapper;

    @Override
    public Page<CarbonContribution> queryAllPageByDTO(PageDTO pageDTO) {
        return null;
    }


    @Override
    public Map<String, Integer> getCarbonTotal() {
        HashMap<String, Integer> stringIntegerHashMap = new HashMap<>();
        //园区植被总和
        Integer vegetationTotal = contributionMapper.getVegetationTotal();
        stringIntegerHashMap.put("全区植被总和", vegetationTotal);
        //园区净化设备
        Integer parkPurificationDeviceTotal = contributionMapper.getParkPurificationDeviceTotal();
        stringIntegerHashMap.put("园区净化设备", parkPurificationDeviceTotal);

        //第三方工程
        Integer tripartiteEngineeringTotal = contributionMapper.getTripartiteEngineeringTotal();
        stringIntegerHashMap.put("第三方工程", tripartiteEngineeringTotal);
        return stringIntegerHashMap;
    }

    /**
     * 查询当前月的前六个月碳综合值
     *
     * @return
     */
    @Override
    public List<Map<String, Integer>> getDataTotal() {
        ArrayList<Map<String, Integer>> maps = new ArrayList<>();
        //获取当前年月
        Calendar calendar = Calendar.getInstance();
        //月份
        int month = calendar.get(Calendar.MONTH) + 1;
        String toMonth = String.valueOf(month);
        //年份
        int year = calendar.get(Calendar.YEAR);
        String toYear = String.valueOf(year);
        String date = String.join("-", toYear, toMonth);
        //要获取的六个月份
        List<String> sixMonth = getSixMonth(date);
        //六个月不需要包含当前月
        sixMonth.remove(date);
        for (String s : sixMonth) {
            Integer vegetationMouth = contributionMapper.getVegetationMouth(s);
            if (vegetationMouth == null) {
                vegetationMouth = 0;
            }
            Integer parkPurificationDeviceMouth = contributionMapper.getParkPurificationDeviceMouth(s);
            if (parkPurificationDeviceMouth == null) {
                parkPurificationDeviceMouth = 0;
            }
            Integer tripartiteEngineeringMouth = contributionMapper.getTripartiteEngineeringMouth(s);
            if (tripartiteEngineeringMouth == null) {
                tripartiteEngineeringMouth = 0;
            }
            HashMap<String, Integer> stringIntegerHashMap = new HashMap<>();
            stringIntegerHashMap.put(s, vegetationMouth + parkPurificationDeviceMouth + tripartiteEngineeringMouth);
            maps.add(stringIntegerHashMap);
        }
        return maps;
    }


    /**
     * * 获取某一月份的前六个月
     * * @param date 日期,格式:"2018-10"
     * * @return
     */
    public List<String> getSixMonth(String date) {
        //返回值
        List<String> list = new ArrayList<String>();
        int month = Integer.parseInt(date.substring(5, 7));
        int year = Integer.parseInt(date.substring(0, 4));
        for (int i = 6; i >= 0; i--) {
            if (month > 7) {
                if (month - i >= 10) {
                    list.add(year + "-" + String.valueOf(month - i));
                } else {
                    list.add(year + "-0" + String.valueOf(month - i));
                }
            } else {
                if (month - i <= 0) {
                    if (month - i + 12 >= 10) {
                        list.add(String.valueOf(year - 1) + "-" + String.valueOf(month - i + 12));
                    } else {
                        list.add(String.valueOf(year - 1) + "-0" + String.valueOf(month - i + 12));
                    }
                } else {
                    if (month - i >= 10) {
                        list.add(String.valueOf(year) + "-" + String.valueOf(month - i));
                    } else {
                        list.add(String.valueOf(year) + "-0" + String.valueOf(month - i));
                    }
                }
            }
        }
        return list;
    }


}
